02. Python vs. C++
How the Lesson is Organized
This lesson has two parts. In the first part, we show you a complete Python class and its equivalent in C++. You'll get an overview of what C++ classes look like and how to use them.
Then, in the second part, you'll code your own C++ class building up the code piece by piece. By the end of the lesson, you should feel comfortable reading C++ object oriented code and writing a basic class; however, remember that learning a new programming language takes practice. And C++ is often considered a language that takes years to master.
Example: Python Class
Let's go directly to an example. Below is the code for a Python class called 'Gaussian'.
You learned about Gaussian distributions and saw the Gaussian equation earlier in the nanodegree. This class stores the values for the standard deviation and mean. The class also has methods for calculating the probability density function, the sum of two gaussians, and the product of two gaussians.
The class contains two class variables called mu
, which is the average and sigma2
, which is the variance.
Here is a summary of the three methods contained in the class:
evaluate
, which represents the probability density function: \LARGE\frac{1}{\sqrt{2\pi\sigma^2}}e^{-\frac{1}{2}\frac{(x - \mu)^2}{\sigma^2}}
In this formula \sigma^2 is the variance.
multiply
, which multiplies two independent Gaussian distributions togetheradd
, which adds two independent Gaussian distributions together
Read through the code so that you understand how the class works.
Python Code for a Gaussian Class
class Gaussian():
def __init__(self, mean, variance):
self.mu = mean
self.sigma2 = variance
def evaluate(self, x):
coefficient = 1.0 / sqrt(2.0 * pi * self.sigma2)
exponential = exp(-0.5 * (x-self.mu) ** 2 / self.sigma2)
return coefficient * exponential
def multiply(self, other):
# calculate new mean
denominator = self.sigma2 + other.sigma2
numerator = self.mu * other.sigma2 + other.mu * self.sigma2
new_mu = numerator / denominator
# calculate new variance
new_var = 1.0 / ( (1.0 / self.sigma2) + (1.0 / other.sigma2) )
# generate new gaussian
return Gaussian(new_mu, new_var)
def add(self, other):
new_mu = self.mu + other.mu
new_sigma2 = self.sigma2 + other.sigma2
return Gaussian(new_mu, new_sigma2)
Example: C++ Class
Now, you will take a look at an equivalent class in C++. Like in other cases you have already seen, the C++ code is longer and has aspects that the Python version did not have. It takes more time and practice to learn object oriented programming in C++ compared to Python.
For example, you will notice that in the C++ class, all of its variables and all of its functions need to be declared first before writing the implementation. The class also has a part labeled private
and another part labeled public
. Furthermore, the C++ class includes extra functions like setMu
, setSigma2
, getMu
, and getSigma2
.
You are going to learn about all of these differences in this lesson. For now, read through the code and see if you can figure out what the set functions and get functions do. Then answer the quiz at the bottom of the page.
#include <math.h>
class Gaussian
{
// private variable declaration
private:
float mu, sigma2;
// public variable and function declarations
public:
// constructor functions
Gaussian ();
Gaussian (float, float);
// change value of average and standard deviation
void setMu(float);
void setSigma2(float);
// output value of average and standard deviation
float getMu();
float getSigma2();
// functions to evaluate
float evaluate (float);
Gaussian multiply (Gaussian);
Gaussian add (Gaussian);
};
// constructor function definitions
Gaussian::Gaussian() {
mu = 0;
sigma2 = 1;
}
Gaussian::Gaussian (float average, float sigma) {
mu = average;
sigma2 = sigma;
}
// set function definitions
void Gaussian::setMu (float average) {
mu = average;
}
void Gaussian::setSigma2 (float sigma) {
sigma2 = sigma;
}
// get function definitions
float Gaussian::getMu () {
return mu;
}
float Gaussian::getSigma2() {
return sigma2;
}
// evaluate function definition
float Gaussian::evaluate(float x) {
float coefficient;
float exponential;
coefficient = 1.0 / sqrt (2.0 * M_PI * sigma2);
exponential = exp ( pow (-0.5 * (x - mu), 2) / sigma2 );
return coefficient * exponential;
}
// multiply function definition
Gaussian Gaussian::multiply(Gaussian other) {
float denominator;
float numerator;
float new_mu;
float new_var;
denominator = sigma2 + other.getSigma2();
numerator = mu * other.getSigma2() + other.getMu() * sigma2;
new_mu = numerator / denominator;
new_var = 1.0 / ( (1.0 / sigma2) + (1.0 / other.sigma2) );
return Gaussian(new_mu, new_var);
}
// add function definition
Gaussian Gaussian::add(Gaussian other) {
float new_mu;
float new_sigma2;
new_mu = mu + other.getMu();
new_sigma2 = sigma2 + other.getSigma2();
return Gaussian(new_mu, new_sigma2);
}
Set Functions